/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package frontend;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import middleware.Item;
import middleware.Tag;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.eclipse.jetty.server.Handler;

/**
 *
 * @author chanhlt
 */
public class MiddlewareHandler implements middleware.MiddlewareFrontend.Iface {

    TTransport transport;
    middleware.MiddlewareFrontend.Client client;

    void init() throws IOException {

        Properties pro = new Properties();
        pro.load(new FileInputStream("src/conf/config.ini"));
        String host = pro.getProperty("host");
        int port = Integer.parseInt(pro.getProperty("port"));
        transport = new TSocket(host, port);
        TFramedTransport framedTransport = new TFramedTransport(transport);
        TProtocol protocol = new TBinaryProtocol(framedTransport);
        client = new middleware.MiddlewareFrontend.Client(protocol);

    }

    @Override
    public List<Tag> getAllTag() throws TException {
        try {
            init();
        } catch (IOException ex) {
            Logger.getLogger(MiddlewareHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        List<Tag> lisTag;
        
        transport.open();
        lisTag=client.getAllTag();
        transport.close();
        
        return lisTag;     
    }

    @Override
    public boolean insertTag(String tagName) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean deleteTag(String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean deleteAllTag(List<String> tagIDs) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean editTag(String tagID, String tagName) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Tag getTag(String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void setViewCountTag(String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Tag> getTopTags(long number) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getAllItems(long number) throws TException {
        try {
            init();
        } catch (IOException ex) {
            Logger.getLogger(MiddlewareHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        List<Item> listItem;
        
        transport.open();
        listItem=client.getAllItems(number);
        transport.close();
        
        return listItem;    
    }

    @Override
    public List<Item> getAllItemshaveTag(String tagID, int numberItems) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> pagingItemsTag(String tagID, int pageNumber, int numberItems) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<String> getAllItemsIDhaveTag(String tagID) throws TException {
        try {
            init();
        } catch (IOException ex) {
            Logger.getLogger(MiddlewareHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        List<String> listItemID;
        
        transport.open();
        listItemID=client.getAllItemsIDhaveTag(tagID);
        transport.close();
        
        return listItemID;
    }

    @Override
    public Item getRandomItem() throws TException {
        try {
            init();
        } catch (IOException ex) {
            Logger.getLogger(MiddlewareHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        transport.open();
        Item resItem=client.getRandomItem();
        transport.close();
        
        return resItem;
        
    }

    @Override
    public Item getRandomItemhaveTag(String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void increaseViewCountItem(String itemID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   

    @Override
    public boolean deleteItem(String itemID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean deleteAllItem(List<String> itemIDs) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean editItem(String itemID, String newItemValue, List<String> newTagIDs) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   

    @Override
    public List<Item> getItemKeyword(String keyWord) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getItemKeywordTag(String keyWord, String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getTopItems(long number) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getTopItemsofTag(long number, String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean insertFavoriteItem(List<Item> listFavoriteItems, Item item) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean deleteFavoriteItem(List<Item> listFavoriteItems, Item item) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean blockUser(String userName) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean unblockUser(String userName) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

   

    @Override
    public boolean deleteUser(String usrName) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Item getItemFromItemID(String itemID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getItemsFromListItemID(List<String> itemIDs) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void increaseLikeCountItem(String itemID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void increaseDislikeCountItem(String itemID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String insertItem(String content, List<String> tagIDs) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getItemsPage(long pageNumber, long itemNumber, String tagID) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<Item> getFavoriteItems() throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean addUser(String userID, String userToken, int userRole) throws TException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
